<?php 
  require_once("../includes/config.php"); 
  

  $length = 20;
  
  if( isset($_GET['page'])  && !empty($_GET['page'] ))
  {
    $page =  filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT);
    $start = $page*20 - 20;
  }
  else
  {
    $start = 0;
    $page = 1;
  }
  
  $binds = array();
  
  $sql = "SELECT *  FROM furnitures
  LEFT JOIN apartments ON 
    furnitures.apartments_id=apartments.id 
  WHERE apartments.owner_id = ?";

  $sqlCount = "SELECT COUNT(*) FROM furnitures
  LEFT JOIN apartments ON 
    furnitures.apartments_id=apartments.id 
  WHERE apartments.owner_id = ?";
  
  $binds['owner_id'] = $_SESSION['user_id'];
  if( isset($_GET['apart'])  && $_GET['apart'] != -1)
  {
    $sql .= " and apartments_id = ?";
    $sqlCount .= " and apartments_id = ?";
    $binds['id'] = $_GET['apart'];
  }
  else
  {
	//Do nothing
  }
  $sql .= " LIMIT ?,? ";
  
  $binds['start']=$start;
  $binds['length'] = $length;
  
  
  $furnitures = query($sql,$binds);
  $Count = query($sqlCount,$binds);
  
  $totalPages = $Count[0]['COUNT(*)'];

  
  
  render("furnitures.php", array("title"=>"الاثاثات","furnitures"=>$furnitures,"totalPages"=>$totalPages,"page"=>$page,"binds"=>$binds,"furns_name"=>$furns_name,"furn_status"=>$furn_status));
?>